package com.liulihui.web.exceptionhandler;

import com.liulihui.web.vo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName: MyExceptionHandler
 * @Description:
 * @Author liulihui
 * @Date: 2021/8/4 12:25
 * @URL：
 * @Version 1.0
 */
//全局异常处理器    如果controller类发生了异常.则来通知这个类来处理异常.
@ControllerAdvice
public class MyExceptionHandler {

    public Logger logger= LoggerFactory.getLogger(MyExceptionHandler.class);
    @ResponseBody
    @ExceptionHandler(RuntimeException.class) //这才是关键
    public Result catchException(RuntimeException e){

        //记录下异常信息
        //使用日志来记录
        logger.error("发生了服务器异常",e);
        return Result.serverError();

    }
}